package es.shinra.app.BBDD.Object.DAO;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.xmldb.api.base.*;
import org.xmldb.api.*;



/**
 *
 * @author Huchnet
 */
public class XMLDbDataSource implements iXMLDbDataSource{


    public Collection getCollection() {
        Collection col = null;
        try {
            String driver = "org.exist.xmldb.DatabaseImpl"; //Driver
            Class cl = Class.forName(driver); //Cargar el driver
            Database database = (Database) cl.newInstance(); //Instancia de la BD
            database.setProperty("create-database", "true");
            DatabaseManager.registerDatabase(database); //Registrar la BD
            //Accedemos a la colección
            col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/App");
            return col;
        } catch (XMLDBException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
        return col;
    }

    public Collection getCollection(String aUserName, String aPassword)   {

        //Falta poner UserName y Password
      //String uri ="xmldb:exist://localhost:8080/exist/xmlrpc/db/App";
        String uri ="xmldb:exist://localhost:8080/exist/xmlrpc/db/App";
        String driver = "org.exist.xmldb.DatabaseImpl";
        Class cl = null;
        try {
            cl = Class.forName(driver);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        Database database = null;
        try {
            database = (Database) cl.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        try {
            DatabaseManager.registerDatabase(database);
        } catch (XMLDBException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
        Collection col = null;
        try {
            col = DatabaseManager.getCollection(uri);
        } catch (XMLDBException ex) {
            Logger.getLogger(XMLDbDataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
	    return col;



    }
}

    